﻿using System;
using System.Data;
using System.Linq;
using ZHDJ.Data.BLL;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;

namespace ZHDJ.Web.Portal
{
    public partial class BranchIndex : PortalPage
    {
        public const string PhotoWallCacheKey = "PARTY_BranchIndex_PhotoWall_DATASOURCE";

        public string BranchID
        {
            get
            {
                return this.Request["BranchID"] ?? "";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        #region 数据操作
        private void BindData()
        {

        }
        private void LoadData()
        {
            LoadBranchInfo();
            //组织动态图
            rpBranchActivitieImg.DataSource = PartyBranchActivitie.GetBranchActivitieTitle(this.BranchID, "limit 0,6");
            rpBranchActivitieImg.DataBind();

            //组织动态
            rpBranchActivitie.DataSource = PartyBranchActivitie.GetBranchActivitieTitle(this.BranchID, "limit 0,6");
            rpBranchActivitie.DataBind();

            rpBranchActivitieLi.DataSource = PartyBranchActivitie.GetBranchActivitieTitle(this.BranchID, "limit 0,6");
            rpBranchActivitieLi.DataBind();

            rpBranchActivitieContent.DataSource = PartyBranchActivitie.GetBranchActivitieTitle(this.BranchID, "limit 0,6");
            rpBranchActivitieContent.DataBind();

            //志愿活动
            rpVolunteerActivity.DataSource = PartyVolunteerActivitie.GetActiviteTitle(this.BranchID, "limit 0,5").Tables[0];
            rpVolunteerActivity.DataBind();

            //志愿心语
            rpQuotation.DataSource = PartyVolunteerQuotation.GetQuotationTitle(this.BranchID, "limit 0,5").Tables[0];
            rpQuotation.DataBind();

            //党务公开
            rpPartyPublic.DataSource = PartyArticle.GetPartyPublicData(this.BranchID);
            rpPartyPublic.DataBind();

            //村务公开
            rpVillagePublic.DataSource = PartyArticle.GetVillagePublicData(this.BranchID);
            rpVillagePublic.DataBind();

            //财务公开
            rpFinancePublic.DataSource = PartyArticle.GetFinancePublicData(this.BranchID);
            rpFinancePublic.DataBind();

            //留影墙
            //if (!CacheHelper.IsExist(PhotoWallCacheKey))
            //{
            //    CacheHelper.Add(PhotoWallCacheKey, PartyPhotoWallImage.GetBranchPhotoWallList(10, this.BranchID), DateTime.Now.AddHours(1));
            //}

            //DataTable dt = (DataTable)CacheHelper.GetCache(PhotoWallCacheKey);
            DataTable dt = PartyPhotoWallImage.GetBranchPhotoWallList(10, this.BranchID, "");
            this.rptHonorHall.DataSource = dt;
            this.rptHonorHall.DataBind();

            //党员排名
            rpRankMember.DataSource = PartyScoreHelper.GetScoreRankMemberByBranchId(0, 10, BranchID);
            rpRankMember.DataBind();

            //组织排名
            rpRankBranch.DataSource = PartyScoreHelper.GetScoreRankBranch(0, 10, BranchID, "");
            rpRankBranch.DataBind();

            //党费
            rpMemberCost.DataSource = PartyMemberCost.GetMemberCost(0, 40, BranchID);
            rpMemberCost.DataBind();

            //先进典型
            rpMassHonour.DataSource = PartyArticle.GetMassHonourArticle(0, 40,"4","",this.BranchID);
            rpMassHonour.DataBind();

            //心得体会
            rpExperience.DataSource= PartyArticle.GetPartySoundArticle(0, 5, BranchID, "", "Experience","" );
            rpExperience.DataBind();

            //思想汇报
            rpThoughtReport.DataSource = PartyArticle.GetPartySoundArticle(0, 5, BranchID, "", "ThoughtReport", "");
            rpThoughtReport.DataBind();
        }

        public void LoadBranchInfo()
        {
            var branch = DB.PartyOrgBranchs.Find(BranchID);
            if (branch == null)
            {
                RedirectTo("Login", "~/default.aspx");
            }
            //this.litBranchName.Text = string.Format(" <a href='../BranchIndex.aspx?BranchID={0}' class='abranchname'>{1}</a>", BranchID, branch.BranchName);
            this.litBranchName.Text = branch.BranchName;
            litMemberCount.Text = DB.PartyOrgMembers.Count(a => a.runSate == "0" && a.BranchID == BranchID).ToString();
            if (branch.BranchMark == "3" || branch.BranchMark == "5")
            {
                BranchScore.Visible = false;
            }
            if (!string.IsNullOrEmpty(branch.ImageUrl))
            {
                ImageAuto.ImgUrl = UpFileBusiness.GetFileUrl(branch.ImageUrl);
            }
            var dt = PartyScoreHelper.QueryBranchScore(BranchID);
            if (dt.Rows.Count > 0)
            {
                this.litRank.Text = Convert.ToString(dt.Rows[0]["Rank"]);
                this.litScore.Text = Convert.ToString(dt.Rows[0]["Score"]);
            }
            else
            {
                this.litRank.Text = "没有排名";
                this.litScore.Text = "0";
            }
        }
    

        
        #endregion

    }
}